//
//  RGCommentToolBarView.m
//  RedGrass
//
//  Created by wukong on 2018/12/24.
//  Copyright © 2018年 hongcaosp. All rights reserved.
//

#import "RGCommentToolBarView.h"
#import <UITextView+Placeholder.h>
#import "NSNotification+Extension.h"
#import "NSAttributedString+Extension.h"


static const CGFloat kCommentTextViewLeftInset               = 15;
static const CGFloat kCommentTextViewRightInset              = 15;
static const CGFloat kCommentTextViewTopBottomInset          = 15;

@interface RGCommentToolBarView()<UITextViewDelegate ,UIGestureRecognizerDelegate>
 
//@property (strong, nonatomic) UITextView *textView;

@property (nonatomic, assign) CGFloat            textHeight;
@property (nonatomic, assign) CGFloat            keyboardHeight;
@property (nonatomic, retain) UILabel            *placeholderLabel;
@property (nonatomic, assign) BOOL isShowing;
//@property (nonatomic, strong) UIImageView        *atImageView;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) UIVisualEffectView *visualEffectView;


@end

@implementation RGCommentToolBarView

- (instancetype)init {
    self = [super init];
    if(self) {
        self.frame = kScreen_Bounds;
        self.backgroundColor = UICOLOR_WITH_RGBINT_A(0x000000, 0.0);
//        self.backgroundColor = UICOLOR_WITH_RGBINT(0x000000);
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGuesture:)]];
        
        _container = [[UIView alloc] initWithFrame:CGRectMake(0, KScreenHeight - 50 - SafeAreaBottomHeight, KScreenWidth, 50 + SafeAreaBottomHeight)];
        _container.backgroundColor = [UIColor whiteColor];
        [self addSubview:_container];
        
        _keyboardHeight = SafeAreaBottomHeight;
        
        _textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0.3, KScreenWidth, 49.7)];
        _textView.backgroundColor = [UIColor clearColor];
        
        _textView.clipsToBounds = NO;
        _textView.textColor = [UIColor whiteColor];
        _textView.font = FONT(customFontName, FFont_14);
        _textView.returnKeyType = UIReturnKeySend;
        _textView.scrollEnabled = NO;
        _textView.placeholder = @"@说点什么......";
        _textView.textContainer.lineBreakMode = NSLineBreakByTruncatingTail;
        _textView.textContainerInset = UIEdgeInsetsMake(kCommentTextViewTopBottomInset, kCommentTextViewLeftInset, kCommentTextViewTopBottomInset, kCommentTextViewRightInset);
        _textView.textContainer.lineFragmentPadding = 0;
        _textHeight = ceilf(_textView.font.lineHeight);

        _line = [[UIView alloc]init];
        _line.backgroundColor = UICOLOR_WITH_RGBINT(0xcccccc);
        _line.frame = CGRectMake(0, 0, KScreenWidth, 0.3);
        [_container addSubview:_line];
//        _atImageView = [[UIImageView alloc] init];
//        _atImageView.contentMode = UIViewContentModeCenter;
//        _atImageView.image = [UIImage imageNamed:@"iconWhiteaBefore"];
//        [_textView addSubview:_atImageView];
        [_container addSubview:_textView];
        _textView.delegate = self;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
//    _atImageView.frame = CGRectMake(KScreenWidth - 50, 0, 50, 50);
//
//    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10.0f, 10.0f)];
//    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
//    [shape setPath:rounded.CGPath];
//    _container.layer.mask = shape;
    
    [self updateTextViewFrame];
}


- (void)updateTextViewFrame {
    CGFloat textViewHeight = _keyboardHeight > SafeAreaBottomHeight ? _textHeight + 2*kCommentTextViewTopBottomInset : ceilf(_textView.font.lineHeight) + 2*kCommentTextViewTopBottomInset;
    _textView.frame = CGRectMake(0, 0, KScreenWidth, textViewHeight);
    _container.frame = CGRectMake(0, KScreenHeight - _keyboardHeight - textViewHeight, KScreenWidth, textViewHeight + _keyboardHeight);
}

//keyboard notification
- (void)keyboardWillShow:(NSNotification *)notification {
    _keyboardHeight = [notification keyBoardHeight];
    [self updateTextViewFrame];
//    _atImageView.image = [UIImage imageNamed:@"iconBlackaBefore"];
    _container.backgroundColor = [UIColor whiteColor];
    _textView.textColor = UICOLOR_WITH_RGBINT(0x333333);
    self.backgroundColor = UICOLOR_WITH_RGBINT_A(0x000000, 0.6);
}

- (void)keyboardWillHide:(NSNotification *)notification {
    _keyboardHeight = SafeAreaBottomHeight;
    [self updateTextViewFrame];
//    _atImageView.image = [UIImage imageNamed:@"iconWhiteaBefore"];
//    _container.backgroundColor = UICOLOR_WITH_RGBINT_A(0x000000, 0.4);
//    _textView.textColor = [UIColor whiteColor];
    self.backgroundColor = [UIColor clearColor];
}

//textView delegate
-(void)textViewDidChange:(UITextView *)textView {
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:textView.attributedText];
    
    if(!textView.hasText) {
        [_placeholderLabel setHidden:NO];
        _textHeight = ceilf(_textView.font.lineHeight);
    }else {
        [_placeholderLabel setHidden:YES];
        _textHeight = [attributedText multiLineSize:KScreenWidth - kCommentTextViewLeftInset - kCommentTextViewRightInset].height;
    }
    [self updateTextViewFrame];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if([text isEqualToString:@"\n"]) {
        if([_delegate respondsToSelector:@selector(onSendText: WithView:)]) {
            [_delegate onSendText:textView.text WithView:self];
            [_placeholderLabel setHidden:NO];
            textView.text = @"";
            _textHeight = ceilf(textView.font.lineHeight);
            [textView resignFirstResponder];
        }
        return NO;
    }
    return YES;
}

-(void) setIsShowBackGround:(BOOL)isShowBackGround{
    _isShowBackGround = isShowBackGround;
    if(isShowBackGround){
        self.backgroundColor = UICOLOR_WITH_RGBINT_A(0x000000, 0.3);
    }else{
        self.backgroundColor = [UIColor clearColor];
    }
}

-(BOOL) isShow{
    return self.isShowing;
}

//handle guesture tap
- (void)handleGuesture:(UITapGestureRecognizer *)sender {
    CGPoint point = [sender locationInView:_textView];
    if(self.isShowBackGround){
        [self dismiss];
    }else{
        if(![_textView.layer containsPoint:point]) {
            [_textView resignFirstResponder];
        }
    }
    
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *hitView = [super hitTest:point withEvent:event];
    if(hitView == self){
        if(!_isShowBackGround) {
            return nil;
        }else{
            return hitView;
        }
    }
    if(self.toolBarTextViewTouchedBlock && [hitView isKindOfClass:[UITextView class]]){
        self.toolBarTextViewTouchedBlock();
        
    }
    NSLog(@"23333333");
    return hitView;
}

//update method
- (void)show {
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    [window addSubview:self];
    _isShowing = YES;
}

- (void)dismiss {
    [self removeFromSuperview];
    _isShowing = NO;

}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//-(UITextView *)textView{
//    if(!_textView){
//        _textView = [[UITextView alloc]init];
//        _textView.placeholder = @"@说点什么";
//        _textView.textColor = UICOLOR_WITH_RGBINT(0x333333);
//        _textView.font = FONT(customFontName, FFont_14);
//    }
//    return _textView;
//}


@end
